package com.dyoud.api.shop.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dyoud.api.ReturnInfo;
import com.dyoud.api.shop.service.ShopApiService;
import com.dyoud.api.shop.shopInfo.dto.AddShopInfoDto;
import com.dyoud.api.shop.shopInfo.dto.ShopPageQueryDto;
import com.dyoud.api.shop.shopInfo.dto.UpdateShopInfoDto;
import com.dyoud.core.ResponseBean;
import com.dyoud.shop.api.model.ShopInCome;
import com.dyoud.shop.api.model.ShopInfo;
import com.dyoud.shop.api.request.ShopInComeRequest;

/**
 * 店铺信息控制器
 * 
 * @author wangshuaifei
 * @date 2018/03/01
 *
 */
@RestController
@RequestMapping("shop")
public class ShopController {

    @Resource
    private ShopApiService shopApiService;

    /**
     * 添加店铺信息
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseBean addShopInfo(@RequestBody @Validated AddShopInfoDto dto) {
        if (StringUtils.isEmpty(dto.getName())) {
            return new ResponseBean().fail(ReturnInfo.SHOP_NAME_NULL_CODE, ReturnInfo.SHOP_NAME_NULL_MESSAGE);
        }
        if (shopApiService.addShopInfo(dto)) {
            return new ResponseBean().success();
        } else {
            return new ResponseBean().fail(ReturnInfo.ADD_SHOP_CODE, ReturnInfo.ADD_SHOP_MESSAGE);
        }
    };

    /**
     * 删除店铺，只是禁用店铺状态
     * 
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/{shopId}", method = RequestMethod.DELETE)
    public ResponseBean delShopInfo(@PathVariable("shopId") Long shopId) {
        int i = shopApiService.delShopInfo(shopId);
        return i > 0 ? new ResponseBean().success() : new ResponseBean().fail(ReturnInfo.DEL_SHOP_CODE, ReturnInfo.DEL_SHOP_MESSAGE);
    };

    /**
     * 更新店铺，修改店铺信息
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/{shopId}", method = RequestMethod.PUT)
    public ResponseBean updateShopInfo(@PathVariable("shopId") Long shopId, @RequestBody UpdateShopInfoDto dto) {
        dto.setShopId(shopId);
        if (ObjectUtils.isEmpty(dto)) {
            return new ResponseBean().fail(ReturnInfo.ADD_SHOP_CODE, ReturnInfo.ADD_SHOP_MESSAGE);
        }
        if (shopApiService.updateShopInfo(dto)) {
            return new ResponseBean().success();
        } else {
            return new ResponseBean().fail(ReturnInfo.UPDATE_SHOP_CODE, ReturnInfo.UPDATE_SHOP_MESSAGE);
        }
    };

    /***
     * 根据店铺id查询单个店铺信息
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{shopId}", method = RequestMethod.GET)
    public ResponseBean getByShopId(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,@PathVariable("shopId") Long shopId) {
        ShopInfo dto = shopApiService.getByShopId(shopId);
        if (dto == null || dto.getShopId() == null) {
            new ResponseBean().fail(ReturnInfo.SHOP_NULL_CODE, ReturnInfo.SHOP_NULL_MESSAGE);
        }
        
        return new ResponseBean().success(shopApiService.getByShopInfo(pageNo, pageSize, shopId));
    }

    /**
     * 分页查询店铺，根据店铺名称和店铺是否被禁用的状态
     * 
     * @param query
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseBean selectByNameAndState(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, ShopPageQueryDto query) {
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        return new ResponseBean().success(shopApiService.selectByNameAndState(query));
    };

    /**
     * 根据cityNo 市，县
     * 
     * @param cityNo
     * @return
     */
    @RequestMapping(value = "/city/{cityNo}", method = RequestMethod.GET)
    public ResponseBean selectByCityNo(@PathVariable("cityNo") Long cityNo) {
        return new ResponseBean().success(shopApiService.selectByCityNo(cityNo));

    }

    /**
     * 获取所有省份
     * 
     * @param cityNo
     * @return
     */
    @RequestMapping(value = "/city", method = RequestMethod.GET)
    public ResponseBean selectByCityNo() {
        return new ResponseBean().success(shopApiService.selectProvince());
    }

    /**
     * 分页查询店铺，根据店铺名称,店铺状态,店铺所在城市
     * 
     * @param query
     * 
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResponseBean selectByNameAndStateAndCity(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, ShopPageQueryDto query) {
         query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        return new ResponseBean().success(shopApiService.selectByNameAndStateAndCity(query));
    }
    
    /*
     * @RequestMapping(value = "/put", method = RequestMethod.GET) public ResponseBean uploadFile(@RequestParam("pic") MultipartFile multipartFile){
     * ResponseBean response = new ResponseBean(); try { InputStream input = multipartFile.getInputStream(); // 原始文件名 String originalName =
     * multipartFile.getOriginalFilename(); String suffix = FileUtils.getSuffix(originalName); byte[] data = IOUtils.toByteArray(input); String key =
     * fileUploadService.upload(data, FileUtils.getNewFileName() + suffix); response.success(key); } catch (IOException e) {
     * log.error(ReturnInfo.UPLOAD_FILE_MSG, e); response.fail(ReturnInfo.UPLOAD_FILE_CODE, ReturnInfo.UPLOAD_FILE_MSG); } return response; };
     */
    @RequestMapping(value = "/{shopId}/shopIncome", method = RequestMethod.GET)
    public ResponseBean shopIncomeList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @PathVariable("partnerId") Long partnerId,ShopInComeRequest dto) {
        dto.setPageNo(pageNo);
        dto.setPageSize(pageSize);
        dto.setShopId(partnerId);
        return new ResponseBean().success(shopApiService.selectList(dto));
    }

    @RequestMapping(value = "/{shopId}/shopIncome", method = RequestMethod.POST)
    public ResponseBean shopIncomeList(@PathVariable("partnerId") Long partnerId,@RequestBody ShopInCome dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return new ResponseBean().fail(ReturnInfo.PARTNER_NULL_CODE, ReturnInfo.PARTNER_NULL_MESSAGE);
        }
        if (shopApiService.insert(dto)>0){
            return new ResponseBean().success();
        } else {
            return new ResponseBean().fail(ReturnInfo.ADD_PARTNER_CODE, ReturnInfo.ADD_PARTNER_MESSAGE);
        }
    }
    /***
     * 查询所有的店铺类型
     * 
     * @return
     */
    @RequestMapping(value = "/shopType", method = RequestMethod.GET)
    public ResponseBean selectShopTypeAll() {
        // TODO Auto-generated method stub
        return new ResponseBean().success(shopApiService.selectShopTypeAll());
    }
    /***
     * 显示到操作员管理员主页
     * @param partnerId
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/{shopId}/{partnerId}/HomeAmin", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean appDisplayHomeAdmin(@PathVariable("partnerId") Long partnerId, 
            @PathVariable("shopId") Long shopId) {
        return new ResponseBean().success(shopApiService.appDisplayHomeAdmin(partnerId, shopId));
    }
    /**
     * 给予用户收益权预览页面
     * 
     * @param shopId 店铺ID
     * @param phone 用户手机号
     * @param spendingMoney 投资金额(消费多少)
     * @return
     */
    @RequestMapping(value = "/{shopId}/spendingMoney", method = RequestMethod.GET)
    public ResponseBean incomeToUserPre(@PathVariable("shopId") Long shopId, 
            String phone, BigDecimal spendingMoney) {
        return new ResponseBean().success(shopApiService.incomeToUserPre(shopId, phone, spendingMoney));
    }
}
